import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;

public class WebserviceTester {
	
	private static void testGet(String resource) {
		try {

			DefaultHttpClient httpClient = new DefaultHttpClient();
			HttpGet getRequest = new HttpGet(resource);

			HttpResponse response = httpClient.execute(getRequest);

			if (response.getStatusLine().getStatusCode() != 200) {
				throw new RuntimeException("Failed : HTTP error code : "
						+ response.getStatusLine().getStatusCode());
			}

			BufferedReader br = new BufferedReader(
					new InputStreamReader((response.getEntity().getContent())));

			String output;
			System.out.println("\n");
			while ((output = br.readLine()) != null) {
				System.out.println(output);
			}

			httpClient.getConnectionManager().shutdown();

		} catch (ClientProtocolException e) {

			e.printStackTrace();

		} catch (IOException e) {

			e.printStackTrace();
		}
	}
	
	public static void testIsTargetSuitable(String sourceid, String targetid) {
		testGet("http://localhost:8080/likes?sourceid="+sourceid+"&targetid="+
				targetid);
	}
	
	public static void testGetValidTargets(String sourceid, String limit) {
		testGet("http://localhost:8080/likes/users?sourceid="+
				sourceid+"&limit="+limit);
	}
	
	private static void testPost(String resource, MultipartEntity input) {
		try {

			DefaultHttpClient httpClient = new DefaultHttpClient();
			HttpPost postRequest = new HttpPost(resource);
			
			postRequest.setEntity(input);

			HttpResponse response = httpClient.execute(postRequest);

			if (response.getStatusLine().getStatusCode() != 200 && 
					response.getStatusLine().getStatusCode() != 201) {
				throw new RuntimeException("Failed : HTTP error code : "
						+ response.getStatusLine().getStatusCode());
			}

			BufferedReader br = new BufferedReader(
					new InputStreamReader((response.getEntity().getContent())));

			String output;
			System.out.println("\n");
			while ((output = br.readLine()) != null) {
				System.out.println(output);
			}

			httpClient.getConnectionManager().shutdown();

		} catch (MalformedURLException e) {
			e.printStackTrace();

		} catch (IOException e) {
			e.printStackTrace();

		}
	}
	
	public static void testCreateUser(String userid, String genderid, 
			String interestedin) {
		MultipartEntity input = new MultipartEntity(
				HttpMultipartMode.BROWSER_COMPATIBLE);
		try {
			input.addPart("userid", new StringBody(userid));
			input.addPart("genderid", new StringBody(genderid));
			input.addPart("interestedin", new StringBody(interestedin));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}

		testPost("http://localhost:8080/users", input);
	}
	
	public static void testUpdateUserImage(String userid, File fileToUpload) {
		MultipartEntity input = new MultipartEntity(
				HttpMultipartMode.BROWSER_COMPATIBLE);
		try {
			input.addPart("userid", new StringBody(userid));
			FileBody fileBody = new FileBody(fileToUpload, 
					"application/octet-stream");
			input.addPart("data", fileBody);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}

		testPost("http://localhost:8080/users/images", input);
	}
	
	private static void testPut(String resource, MultipartEntity input) {
		try {

			DefaultHttpClient httpClient = new DefaultHttpClient();
			HttpPut putRequest = new HttpPut(resource);
			
			putRequest.setEntity(input);

			HttpResponse response = httpClient.execute(putRequest);

			if (response.getStatusLine().getStatusCode() != 200 && 
					response.getStatusLine().getStatusCode() != 201) {
				throw new RuntimeException("Failed : HTTP error code : "
						+ response.getStatusLine().getStatusCode());
			}

			BufferedReader br = new BufferedReader(
					new InputStreamReader((response.getEntity().getContent())));

			String output;
			System.out.println("\n");
			while ((output = br.readLine()) != null) {
				System.out.println(output);
			}

			httpClient.getConnectionManager().shutdown();

		} catch (MalformedURLException e) {
			e.printStackTrace();

		} catch (IOException e) {
			e.printStackTrace();

		}
	}
	
	public static void testTrainUserBot(String userid, String trainingid, 
			String response) {
		MultipartEntity input = new MultipartEntity(
				HttpMultipartMode.BROWSER_COMPATIBLE);
		try {
			input.addPart("userid", new StringBody(userid));
			input.addPart("trainingid", new StringBody(trainingid));
			input.addPart("response", new StringBody(response));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}

		testPut("http://localhost:8080/users/training", input);
	}
	
	public static void testSetMethod(String userid, String methodid) {
		MultipartEntity input = new MultipartEntity(
				HttpMultipartMode.BROWSER_COMPATIBLE);
		try {
			input.addPart("userid", new StringBody(userid));
			input.addPart("methodid", new StringBody(methodid));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}

		testPut("http://localhost:8080/users/method", input);
	}
	
	public static void testSetInterestedIn(String userid, String interestedin) {
		MultipartEntity input = new MultipartEntity(
				HttpMultipartMode.BROWSER_COMPATIBLE);
		try {
			input.addPart("userid", new StringBody(userid));
			input.addPart("interest", new StringBody(interestedin));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}

		testPut("http://localhost:8080/users/interestedin", input);
	}
	
	public static void testSetGender(String userid, String gender) {
		MultipartEntity input = new MultipartEntity(
				HttpMultipartMode.BROWSER_COMPATIBLE);
		try {
			input.addPart("userid", new StringBody(userid));
			input.addPart("genderid", new StringBody(gender));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}

		testPut("http://localhost:8080/users/gender", input);
	}
	
	private static void testDelete(String resource) {
		try {
			
			DefaultHttpClient httpClient = new DefaultHttpClient();
			HttpDelete deleteRequest = new HttpDelete(resource);

			HttpResponse response = httpClient.execute(deleteRequest);

			if (response.getStatusLine().getStatusCode() != 200 && 
					response.getStatusLine().getStatusCode() != 201) {
				throw new RuntimeException("Failed : HTTP error code : "
						+ response.getStatusLine().getStatusCode());
			}

			BufferedReader br = new BufferedReader(
					new InputStreamReader((response.getEntity().getContent())));

			String output;
			System.out.println("\n");
			while ((output = br.readLine()) != null) {
				System.out.println(output);
			}

			httpClient.getConnectionManager().shutdown();

		} catch (MalformedURLException e) {
			e.printStackTrace();

		} catch (IOException e) {
			e.printStackTrace();

		}
	}
	
	public static void testDeleteUser(String userid) {
		testDelete("http://localhost:8080/users?userid="+userid);
	}

}

